﻿Ext.define("PSI.InPlan.TodayPlanForm", {
    config: {
        today: ""
    },

    constructor: function (config) {
        this.initConfig(config);
    },
    createUI: function () {
        var me = this;
        var yy = me.getToday();
        var ff = Date.parse(yy.replace(/-/g, "/"));
        var dd = new Date(ff);
        var hh = new Date(ff);
        var nn = dd.getDate();
        me.currentDay = nn + 1
        me.mydatetime = ff
        var panel = Ext.create("Ext.container.Container", {
            border: 0,
            layout: "fit",
            items: [me.getInPlanGrid()]
        });

        me.panel = panel;
       
        me.refreshInPlanGrid();
    },

    getMainPanel: function () {
        return this.panel;
    },

    getInPlanGrid: function () {
        var me = this;

        if (!me.__inPlanGrid) {
            me.__inPlanGrid = me.createInPlanGrid();
        }

        return me.__inPlanGrid;
    },

    createInPlanGrid: function () {
        var me = this;
        var modelName = "PSIInPlanModel";
        var fs = ["address", "sum1", "sum2"];
        for (var i = 0; i < me.currentDay; i++) {
            fs.push("f_" + i + "_1");
            fs.push("f_" + i + "_2");
        }
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: fs
        });

        var cols = [{ header: "门点", dataIndex: "address", menuDisabled: true, draggable: false, sortable: false, locked: true }];
        cols.push({
            header: "本月小计", menuDisabled: true, draggable: false, sortable: false, locked: true,
            columns: [
                {
                    header: "计划", dataIndex: "sum1", menuDisabled: true, width: 40, align: "right", draggable: false, sortable: false, locked: true, renderer: function (v) {
                         return "<a href='#'>" + v + "</a>";
                    }
                },
                {
                    header: "完成", dataIndex: "sum2", menuDisabled: true, width: 40, align: "right", draggable: false, sortable: false, locked: true, renderer: function (v) {
                        return "<a href='#'>" + v + "</a>";
                    }
                }
            ]
        });
        var dd = new Date(me.mydatetime);
        for (var i = 0; i < me.currentDay; i++) {
            var prex = Ext.Date.format(new Date(me.mydatetime), "Y-m");
            var cc = new Date(dd.getFullYear(), dd.getMonth() + 1, 0).getDate()
            var day = me.currentDay - i;
            if (day < 10) {
                day = "0" + day;
            }
            if (day > cc) {
                prex = Ext.Date.format(new Date(dd.getFullYear(), dd.getMonth() + 1), "Y-m");
                day = 1;
            }
            cols.push({
                header: prex + "-" + day, menuDisabled: true, draggable: false, sortable: false,
                columns: [
                    {
                        header: "计划", dataIndex: "f_" + i + "_1", menuDisabled: true, width: 40, align: "right", draggable: false, sortable: false, renderer: function (v) {
                            if (v != null && v != "") {
                                return "<a href='#'>" + v + "</a>";
                            }
                            else {
                                return " ";
                            }
                        }
                    },
                    {
                        header: "完成", dataIndex: "f_" + i + "_2", menuDisabled: true, width: 40, align: "right", draggable: false, sortable: false, renderer: function (v) {
                            if (v != null && v != "") {
                                return "<a href='#'>" + v + "</a>";
                            }
                            else {
                                return " ";
                            }
                        }
                    }
                ]
            });
        }

        return Ext.create("Ext.grid.Panel", {
            title: "本月进口计划",
            columnLines: true,
            columns: cols,
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            }),
            listeners: {
                cellclick: function (me, td, cellIndex, record, tr, rowIndex, e, eOpts)
                {
                    var value=$(td).text();
                    if ($.trim(value) == "")
                    {
                        return false;
                    }

                    var cc = $(tr).parent().parent().parent().parent().attr("id");

                    if (cc.indexOf("normal") > 0)
                    {
                        cellIndex=cellIndex+3
                    }
                    var num = cellIndex % 2;

                    if (cellIndex == 0)
                    {
                        return false;
                    }
                    var time
                    if (num == 0) {
                        time = cols[cellIndex / 2].header
                    }
                    else {
                        time = cols[(cellIndex+1) / 2].header
                    }
                    window.location.href = "/InConsignation/ReportDetile?time=" + time + "&address=" + record.raw.address
                }

            }
        });
    },

    onRefreshPlanGird: function () {
        this.refreshInPlanGrid();
    },

    refreshInPlanGrid: function () {
        var me = this;
        var grid = me.getInPlanGrid();
        var store = grid.getStore();

        var el = grid.getEl() || Ext.getBody();
        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/InPlan/GetTodayPlanListData",
            method: "POST",
            callback: function (options, success, response) {
                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    store.add(data);
                }

                el.unmask();
            }
        });
    }
});